import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/modle/main/entity/commo_spec4app_record_entity.dart';
import 'package:shopping_group/modle/more/bloc/more_moneyjuan_bloc.dart';
import 'package:shopping_group/modle/play/ui/xianjinjuan_paly_page.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

class MoreMoneyJuanPage extends StatefulWidget {
  final String byYonJin;
  MoreMoneyJuanPage({Key key, @required this.byYonJin}) : super(key: key);
  @override
  _MoreMoneyJuanPageState createState() => _MoreMoneyJuanPageState();
}

class _MoreMoneyJuanPageState extends State<MoreMoneyJuanPage> {
  List<CommoSpec4AppRecordEntity> _listEntity = [];
  MoreMoneyJuanBloc _bloc;
  @override
  void initState() {
    super.initState();
    _initSync();
  }

  void _initSync() {
    _bloc = MoreMoneyJuanBloc();
    _bloc.initCommSpecData.add(null);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '代理券'),
      body: Container(
        height: double.infinity,
        decoration: new BoxDecoration(
          color: Colours.title_white,
        ),
        child: Column(
          children: <Widget>[
            Offstage(
              offstage: 'Y' != this.widget.byYonJin,
              child: _titleView(),
            ),

            // _buildNotice(),
            Expanded(
              child: _gridView(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _titleView() {
    return Container(
      alignment: Alignment.center,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      decoration: new BoxDecoration(
        color: Colours.blackground_black_239,
      ),
      child: WidgetUtils.getTextColorView(
          '你的推广费账户余额总共为${BaseConstant.integralAccount}元，请选择要购置的代理券',
          Colors.white,
          Dimens.FONT_SIZE_14),
    );
  }

  Widget _gridView() {
    return StreamBuilder(
      stream: _bloc.sendCommSpecData,
      initialData: _listEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is List) {
          _listEntity = object;
        }
        return Container(
          // margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),

          // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
          child: GridView.builder(
              itemCount: _listEntity.length,

              //SliverGridDelegateWithFixedCrossAxisCount 构建一个横轴固定数量Widget
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  //横轴元素个数
                  crossAxisCount: 2,
                  //纵轴间距
                  mainAxisSpacing: 4.0,
                  //横轴间距
                  crossAxisSpacing: 16.0,

                  //子组件宽高长度比例
                  childAspectRatio: 0.75),
              itemBuilder: (BuildContext context, int index) {
                //Widget Function(BuildContext context, int index)

                return getItemContainer(_listEntity[index]);
              }),
        );
      },
    );
  }

  Widget getItemContainer(CommoSpec4AppRecordEntity entity) {
    return GestureDetector(
      onTap: (() {
        RouteUtil.goParameter(XianJinJuanPalyPage(
          id: entity.id,
          byYonJin: this.widget.byYonJin,
        ));
      }),
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
        child: Material(
          borderRadius: BorderRadius.circular(4.0),
          color: Colours.title_white,
          // elevation: 5.0,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Container(
                height: 110.0,
                width: 110.0,
                margin: const EdgeInsets.all(5.0),
                child: FadeInImage.assetNetwork(
                  image:
                      'https://lizhengyouxuan.oss-cn-shanghai.aliyuncs.com/lizhengyouxuan/commSpec/xianjinjuan.png',
                  placeholder: 'images/picture_loading.png' /* 指定gif */,
                  fit: BoxFit.contain,
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    margin: const EdgeInsets.only(top: 12),
                    child: WidgetUtils.getTextMaxLinesView(
                        entity.name, Colours.text_2222, Dimens.FONT_SIZE_14,
                        overflow: TextOverflow.ellipsis),
                  ),
                  SizedBox(
                    height: 4,
                  ),

                  Container(
                    margin: const EdgeInsets.only(bottom: 10),
                    child: WidgetUtils.getTextColorView('价格:${entity.outPrice}',
                        Colours.main_01, Dimens.FONT_SIZE_16,
                        textAlign: TextAlign.start,
                        fontweight: FontWeight.w500),
                  ),
                  // SizedBox(
                  //   width: 4,
                  // ),
                  // Container(
                  //   margin: const EdgeInsets.only(left: 12, bottom: 10),
                  //   child: WidgetUtils.getTextColorView(
                  //       '数量:${entity.hadShopCt}',
                  //       Colours.main_01,
                  //       Dimens.FONT_SIZE_14,
                  //       textAlign: TextAlign.start),
                  // ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
